package com.ruitu.health_1.mvp.presenter;

import com.ruitu.arad.base.BasePresenter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_1.dao.bean.Collect;
import com.ruitu.health_1.mvp.model.UserInfoModel;
import com.ruitu.health_1.ui.AdAdActivity;
import com.ruitu.health_1.ui.AddressActivity;
import com.ruitu.health_1.ui.CollectActivity;
import com.ruitu.health_1.ui.CouponListActivity;
import com.ruitu.health_1.ui.PersonalCenterActivity;
import com.ruitu.health_1.ui.WalletActivity;
import com.ruitu.router_module.bean.BaseResult2;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.cart.CouponList;
import com.ruitu.router_module.bean.user.Address;
import com.ruitu.router_module.bean.user.Wallet;

import java.io.File;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UserInfoPresent {

    public static class AddressListP extends BasePresenter<AddressActivity, UserInfoModel.AddressM> {
        public Disposable addressList(String body) {
            return m.addressList(body)
                    .subscribe(new Consumer<BaseResult2<List<Address>>>() {
                        @Override
                        public void accept(BaseResult2<List<Address>> baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.setData(baseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable deleteAddress(RequestBody body) {
            return m.deleteAddress(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.deleteSuc();
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }

    public static class AddressP extends BasePresenter<AdAdActivity, UserInfoModel.AddressM> {
        public Disposable addAddress(RequestBody body) {
            return m.addAddress(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.finish();
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable updateAddress(RequestBody body) {
            return m.updateAddress(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.finish();
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }

    public static class CouponListP extends BasePresenter<CouponListActivity, UserInfoModel.CouponListM> {
        public Disposable couponList(int pageNum, int pageSize, String memberId, String useStatus) {
            return m.couponList(pageNum, pageSize, memberId, useStatus)
                    .subscribe(new Consumer<BaseResult2<PageModel2<CouponList>>>() {
                        @Override
                        public void accept(BaseResult2<PageModel2<CouponList>> pageModel2BaseResult2) throws Exception {
                            v.hideProgress();
                            if (pageModel2BaseResult2.isSuccess()) {
                                v.setData(pageModel2BaseResult2.getData().getList());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }

    public static class ChangeHeadImgP extends BasePresenter<PersonalCenterActivity, UserInfoModel.ChangeHeadImgM> {

        public MultipartBody changeHeadImg(File file, String userId) {
            MediaType FORM_CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.addPart(MultipartBody.Part.createFormData("userId", null, RequestBody.create(FORM_CONTENT_TYPE, userId)));
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
            builder.addFormDataPart("file", file.getName(), requestBody);
            MultipartBody multipartBody = builder.build();
            return multipartBody;
        }

        public Disposable changeHeadImg(MultipartBody body) {
            return m.changeHeadImg(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.setHeadSuc();
                            }
                            ToastUtils.showShort(baseResult2.getMessage());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable changeUserInfo(RequestBody body) {
            return m.changeUserInfo(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.setSex();
                            }
                            ToastUtils.showShort(baseResult2.getMessage());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }

    public static class CollectListP extends BasePresenter<CollectActivity, UserInfoModel.CollectListM> {
        public Disposable collectList(int pageNum, int pageSize, String memberId) {
            return m.collectList(pageNum, pageSize, memberId)
                    .subscribe(new Consumer<BaseResult2<PageModel2<Collect>>>() {
                        @Override
                        public void accept(BaseResult2<PageModel2<Collect>> pageModel2BaseResult2) throws Exception {
                            v.hideProgress();
                            if (pageModel2BaseResult2.isSuccess()) {
                                v.setData(pageModel2BaseResult2.getData().getList());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }

    public static class WalletP extends BasePresenter<WalletActivity, UserInfoModel.WalletM> {
        public Disposable walletInfo(String id, String type) {
            return m.getWalletInfo(id, type)
                    .subscribe(new Consumer<BaseResult2<Wallet>>() {
                        @Override
                        public void accept(BaseResult2<Wallet> walletBaseResult2) throws Exception {
                            v.hideProgress();
                            if (walletBaseResult2.isSuccess()) {
                                v.getWalletInfo(walletBaseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }
}
